package com.yunxiao.cloud.common.base;

import com.yunxiao.cloud.common.constant.CommonConstant;
import com.yunxiao.cloud.common.constant.ExceptionConstant;

import java.io.Serializable;

/**
 * @description: 统一结果返回类内部rpc
 * @date: 2019/12/11 09:34
 * @author: yunxiao
 * @version: 1.0
 */
public class BaseResultResponse<T> implements Serializable {

    private static final long serialVersionUID = -1626090864537938290L;
    /**
     * 状态码：0成功，其他为失败
     */
    private String code;

    private boolean success;

    /**
     * 成功为success，其他为失败原因
     */
    private String message;

    /**
     * 数据结果集
     */
    private T data;

    public BaseResultResponse() {
    }

    public BaseResultResponse(String code, String message) {
        this.code = code;
        this.message = message;
        if (CommonConstant.SUCCESS.equals(code)){
            success = true;
        }
    }

    public BaseResultResponse(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        if (CommonConstant.SUCCESS.equals(code)){
            success = true;
        }
    }

    public static <T> BaseResultResponse<T> ok() {
        return new BaseResultResponse<>(ExceptionConstant.SERVICE_SUCCESS_CODE, ExceptionConstant.SERVICE_SUCCESS_MSG);
    }

    public static <T> BaseResultResponse<T> ok(T data) {
        return new BaseResultResponse<>(ExceptionConstant.SERVICE_SUCCESS_CODE, ExceptionConstant.SERVICE_SUCCESS_MSG, data);
    }

    public static <T> BaseResultResponse<T> ok(String message) {
        return new BaseResultResponse<>(ExceptionConstant.SERVICE_SUCCESS_CODE, message);
    }

    public static <T> BaseResultResponse<T> ok(String message, T date) {
        return new BaseResultResponse<>(ExceptionConstant.SERVICE_SUCCESS_CODE, message, date);
    }


    public static <T> BaseResultResponse<T> error(String code, String message) {
        return new BaseResultResponse<>(code, message);
    }

    public static <T> BaseResultResponse<T> error(String code, String message, T data) {
        return new BaseResultResponse<>(code, message, data);
    }

    public static <T> BaseResultResponse<T> error(String message) {
        return new BaseResultResponse<>(ExceptionConstant.SERVICE_SYSTEM_ERROR_CODE, message);
    }

    public static <T> BaseResultResponse<T> error(T data) {
        return new BaseResultResponse<>(ExceptionConstant.SERVICE_SYSTEM_ERROR_CODE, ExceptionConstant.SERVICE_SYSTEM_ERROR_MSG, data);
    }

    public static <T> BaseResultResponse<T> error() {
        return new BaseResultResponse<>(ExceptionConstant.SERVICE_SYSTEM_ERROR_CODE, ExceptionConstant.SERVICE_SYSTEM_ERROR_MSG);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}
